package com.freight.order.controller;

import com.alibaba.fastjson.JSONArray;
import com.freight.core.common.annotation.SystemLog;
import com.freight.core.common.enums.LogType;
import com.freight.core.common.utils.PageUtil;
import com.freight.core.common.utils.ResultUtil;
import com.freight.core.common.vo.PageVo;
import com.freight.core.common.vo.Result;
import com.freight.core.common.vo.SearchVo;
import com.freight.order.dto.DocumentaryDto;
import com.freight.order.dto.OrderAmtDto;
import com.freight.order.dto.OrderStatusDto;
import com.freight.order.entity.Documentary;
import com.freight.order.entity.OrderOrder;
import com.freight.order.service.OrderOrderService;
import com.freight.order.vo.OrderProvinceVo;
import com.freight.order.vo.OrderStateCntVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author huyingtai
 */
@Slf4j
@RestController
@Api(description = "运单信息操作接口")
@RequestMapping("/freight/orderOrder")
@Transactional
public class OrderOrderController {

    @Autowired
    private OrderOrderService orderOrderService;

    @RequestMapping(value = "/getOrderProvinceVo", method = RequestMethod.POST)
    @ApiOperation(value = "获取运输中的运单")
    public Result<Map> getOrderProvinceVo(String orderNo) {
        Map map = orderOrderService.getOrderProvinceVo(orderNo);
        return new ResultUtil<Map>().setData(map);
    }

    @RequestMapping(value = "/vehicleListDispatch", method = RequestMethod.POST)
    @ApiOperation(value = "平台端 --- 调度配载 --- 查询车辆位置信息")
    public Result<JSONArray> vehicleListDispatch(Long goodsId, String goodsTypae, String goodsSubTypae, String vehicleState, String range) {
        JSONArray jsonArray = orderOrderService.vehicleListDispatch(goodsId, goodsTypae, goodsSubTypae, vehicleState, range);
        return new ResultUtil<JSONArray>().setData(jsonArray);
    }

    @RequestMapping(value = "/companyVehicleListDispatch", method = RequestMethod.POST)
    @ApiOperation(value = "货主端 --- 调度配载 --- 查询车辆位置信息")
    public Result<JSONArray> companyVehicleListDispatch(Long goodsId, String goodsTypae, String goodsSubTypae, String vehicleState, String range) {
        JSONArray jsonArray = orderOrderService.companyVehicleListDispatch(goodsId, goodsTypae, goodsSubTypae, vehicleState, range);
        return new ResultUtil<JSONArray>().setData(jsonArray);
    }

    @RequestMapping(value = "/vehicleListPosition", method = RequestMethod.POST)
    @ApiOperation(value = "平台端 --- 运力地图 --- 查询车辆位置信息")
    public Result<JSONArray> vehicleListPosition(String goodsTypae, String goodsSubTypae, String vehicleState, String areaCode) {
        JSONArray jsonArray = orderOrderService.vehicleListPosition( goodsTypae, goodsSubTypae, vehicleState, areaCode);
        return new ResultUtil<JSONArray>().setData(jsonArray);
    }

    @RequestMapping(value = "/companyVehicleListPosition", method = RequestMethod.POST)
    @ApiOperation(value = "货主端 --- 运力地图 --- 查询车辆位置信息")
    public Result<JSONArray> companyVehicleListPosition(String goodsTypae, String goodsSubTypae, String vehicleState, String areaCode) {
        JSONArray jsonArray = orderOrderService.companyVehicleListPosition( goodsTypae, goodsSubTypae, vehicleState, areaCode);
        return new ResultUtil<JSONArray>().setData(jsonArray);
    }

    @RequestMapping(value = "/addMobileTrack", method = RequestMethod.POST)
    @ApiOperation(value = "添加手机轨迹")
    public Result<OrderProvinceVo> addMobileTrack(Long orderId,String lat,String lon,String mobileModel) {
        OrderProvinceVo orderProvinceVo = orderOrderService.addMobileTrack( orderId, lat, lon, mobileModel);
        return new ResultUtil<OrderProvinceVo>().setData(orderProvinceVo);
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Page<OrderOrder>> getByCondition(OrderOrder orderOrder, SearchVo searchVo, PageVo pageVo) {
        Page<OrderOrder> page = orderOrderService.findByCondition(orderOrder, searchVo, PageUtil.initPage(pageVo));
        return new ResultUtil<Page<OrderOrder>>().setData(page);
    }

    @RequestMapping(value = "/getOrderStateCnt", method = RequestMethod.GET)
    @ApiOperation(value = "返回订单首页汇总信息")
    public Result< List<OrderStateCntVo>> getUserOrderCntVo() {
        List<OrderStateCntVo> res = orderOrderService.getUserOrderCntVo();
        return new ResultUtil<List<OrderStateCntVo>>().setData(res);
    }

    @RequestMapping(value = "/updateAddressFence", method = RequestMethod.POST)
    @ApiOperation(value = "改变运单地址围栏状态")
    public Result<String> updateAddressFence(String addressFence,Long orderId) {
        orderOrderService.updateAddressFence(addressFence, orderId);
        return new ResultUtil<String>().setData("操作成功");
    }

    @RequestMapping(value = "/changeOrderState", method = RequestMethod.POST)
    @ApiOperation(value = "平台端 --- 改变运单状态")
    @SystemLog(description = "平台改变运单状态", type = LogType.OPERATION)
    public Result<String> changeOrderState(OrderStatusDto orderSateDto) {
        String res = orderOrderService.changeOrderState(orderSateDto);
        return new ResultUtil<String>().setData(res);
    }



    @RequestMapping(value = "/updateStatisticsDetails", method = RequestMethod.POST)
    @ApiOperation(value = "平台端 --- 修改结算单")
    public Result<String> updateStatisticsDetails(@ApiParam("运单id") Long orderId,
                                                  @ApiParam("装车重量") Double transGoodWeight,
                                                  @ApiParam("卸车重量") Double realGoodWeight,
                                                  @ApiParam("司机运费单价") BigDecimal driverUnitPrice,
                                                  @ApiParam("平台运费单价") BigDecimal platformUnitPrice,
                                                  @ApiParam("油费用") BigDecimal oilcostCargo,
                                                  @ApiParam("ETC费用") BigDecimal etcCargo,
                                                  @ApiParam("货损") BigDecimal cargoDamage,
                                                  @ApiParam("实际结算运费") BigDecimal shouldKnot,
                                                  @ApiParam("实结金额") BigDecimal solidKnot,
                                                  @ApiParam("调整金额") BigDecimal adjustment,
                                                  String types,String addReduce,String adjustmentremarks,
                                                  BigDecimal cargoOwnerAdjustment,String cargoOwnerAddReduce,String cargoOwnerAdjustmentremarks){
        orderOrderService.updateStatisticsDetails(types,orderId,transGoodWeight,realGoodWeight,driverUnitPrice,platformUnitPrice,oilcostCargo,etcCargo,
                cargoDamage, shouldKnot,solidKnot,adjustment, addReduce,adjustmentremarks,
                cargoOwnerAdjustment, cargoOwnerAddReduce, cargoOwnerAdjustmentremarks);
        return new ResultUtil<String>().setData("修改成功");
    }

    @RequestMapping(value = "/companyToExamine", method = RequestMethod.POST)
    @ApiOperation(value = "货主端 --- 改变运单状态")
    public Result<String> companyToExamine(OrderStatusDto orderSateDto) {
        String res = orderOrderService.changeOrderState(orderSateDto);
        return new ResultUtil<String>().setData(res);
    }

    @RequestMapping(value = "/orderReceiving", method = RequestMethod.POST)
    @ApiOperation(value = "司机端 --- 改变运单状态")
    public Result<String> orderReceiving(OrderStatusDto orderSateDto){
        String res = orderOrderService.changeOrderState(orderSateDto);
        return new ResultUtil<String>().setData(res);
    }


    @RequestMapping(value = "/uploadOrderPicId", method = RequestMethod.POST)
    @ApiOperation(value = "司机端 --- 上传图片")
    @SystemLog(description = "上传图片", type = LogType.OPERATION)
    public Result<String> uploadReceiptOrderPicId(@RequestParam(required = false) MultipartFile file,
                                                String fileType, Long orderId, String address,Double lng,Double lat) {
        String fId = orderOrderService.uploadReceiptOrderPicId(file, fileType, orderId, address, lng, lat);
        return ResultUtil.data(fId);
    }


    @RequestMapping(value = "/addDocumentary", method = RequestMethod.POST)
    @ApiOperation(value = "平台端 --- 添加跟单记录")
    public Result<String> addDocumentary(DocumentaryDto documentaryDto) {
        orderOrderService.addDocumentary(documentaryDto);
        return new ResultUtil<String>().setData("操作成功");
    }

    @RequestMapping(value = "/getDocumentaryList", method = RequestMethod.POST)
    @ApiOperation(value = "平台端 --- 根据运单id获取跟单列表")
    public Result<List<Documentary>> getDocumentaryList(Long orderId) {
        List<Documentary> page = orderOrderService.getDocumentaryList(orderId);
        return new ResultUtil<List<Documentary>>().setData(page);
    }

    @RequestMapping(value = "/uploadOrderInfo", method = RequestMethod.POST)
    @ApiOperation(value = "修改运单")
    @SystemLog(description = "平台修改运单", type = LogType.OPERATION)
    public Result<String> uploadOrderInfo(OrderAmtDto orderInfoDto) {
        String res = orderOrderService.uploadOrderInfo(orderInfoDto);
        return ResultUtil.data(res);
    }

}
